package com.sohh.demo;

import cn.hutool.core.lang.copier.Copier;
import cn.hutool.core.lang.func.Func1;
import com.sohh.demo.pojo.IMyFunctionInterface;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.*;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @description:
 * @author: ruoan
 * @date: 2021/2/21 15:57
 */
public class Test8 {

    public static void main(String[] args) {
        //List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        ////集合聚合成字符串
        //final String collect = strings.stream().filter(item -> !item.isEmpty()).collect(Collectors.joining(","));
        //System.out.println(collect);
        //
        //
        //List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        //final IntSummaryStatistics intSummaryStatistics
        //        = numbers.stream().mapToInt(x -> x).summaryStatistics();
        //
        //final double average = intSummaryStatistics.getAverage();
        //final int max = intSummaryStatistics.getMax();
        //System.out.println(average);
        //System.out.println((3+2+2+3+7+3+5)/7.0);
        //System.out.println(max);
        //
        //
        //System.out.println("-------------------");
        //System.out.println("-------------------");


        // 字符串转map输出
        //List<String> strings2 = Arrays.asList("abc", "dg", "bc", "efg", "abcd","35c", "jkl");
        //Map<Integer, String> map = new HashMap<>();
        ////map = strings2.stream().collect(Collectors.toMap(item->strings2.indexOf(item), item -> item));
        //map = strings2.stream()
        //        .collect( Collectors.toMap( str -> strings2.indexOf(str), str -> str ) );
        //
        //for (Integer i : map.keySet()) {
        //    System.out.println(i+"->");
        //    System.out.println(map.get(i));
        //    System.out.println("-------------------");
        //}
        //
        //

        //List<String> strings3 = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        //final int bc = strings3.indexOf("");
        //System.out.println(bc);



        class User{
            private String name;
            private Integer score;

            public String getGender() {
                return gender;
            }

            public void setGender(String gender) {
                this.gender = gender;
            }

            private String gender;

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public Integer getScore() {
                return score;
            }

            public void setScore(Integer score) {
                this.score = score;
            }

            public User(String name, Integer score, String gender) {
                this.name = name;
                this.score = score;
                this.gender = gender;
            }

            @Override
            public String toString() {
                return "User{" +
                        "name='" + name + '\'' +
                        ", score=" + score +
                        ", gender='" + gender + '\'' +
                        '}';
            }
        }

        User user1 = new User("zhangsan", 60,"男");
        User user2 = new User("lisi", 80,"女");
        User user3 = new User("zhangsan", 80,"男");
        User user4 = new User("wangwu", 50,"女");
        User user5 = new User("wangwu2", 50,"男");
        User user6 = new User("zhangsan", 80,"男");


        final List<User> users = Arrays.asList(user1, user2, user3, user4,user5,user6);
        users.stream().min(Comparator.comparingInt(User::getScore)).ifPresent(System.out::println);
        final Optional<User> collect = users.stream().max(Comparator.comparingInt(User::getScore));
        collect.ifPresent(System.out::println);


        System.out.println("-------------------");
        // 拼接名字
        final String collect1 = users.stream().map(User::getName).collect(Collectors.joining());
        System.out.println(collect1);


         // 二级分组
        final Map<Integer, Map<String, Map<String, List<User>>>> collect2 =
                users.stream().collect(Collectors.groupingBy(User::getScore, Collectors.groupingBy(User::getName, Collectors.groupingBy(User::getGender))));
        System.out.println(collect2);


        System.out.println("-------------------");
        //分区
        final Map<Boolean, List<User>> collect3 = users.stream().collect(Collectors.partitioningBy(user -> user.getScore() >= 60));
        System.out.println(collect3);

        System.out.println("-------------------");
        //二级分区
        final Map<Boolean, Map<Boolean, List<User>>> collect4 = users.stream().collect(Collectors.partitioningBy(user -> user.getScore() >= 60, Collectors.partitioningBy(user -> user.getScore() >= 80)));
        System.out.println(collect4);

        System.out.println("-------------------");
        //分区,是否及格,算出及格的个数
        final Map<Boolean, Long> collect5 =
                users.stream().collect(Collectors.partitioningBy(user -> user.getScore() >= 60, Collectors.counting()));
        System.out.println(collect5);


        System.out.println("-------------------");
        //先按照名字分组,获取每个分组分数最小的
        final Map<String, User> collect6 = users.stream().collect(
                Collectors.groupingBy(
                        User::getName, Collectors.collectingAndThen(
                                Collectors.maxBy(Comparator.comparingInt(User::getScore)), Optional::get)));

        System.out.println(collect6);
        System.out.println("-------------------");
        final Integer collect7 = users.stream().mapToInt(User::getScore).sum();
        System.out.println(collect7);

        System.out.println("-------------------");
        User u = new User("发几个",26,"女");
        final IMyFunctionInterface<User, Integer> getScore = User::getScore;
        final Callable<Integer> getScore1 = u::getScore;


        System.out.println("-------------------");






    }



}
